﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Cm.Web.Common.EnhancedViewLocations;
using Cm.Web.Common;
using System.Globalization;
using System.Threading;

namespace Cm.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            //其他操作
            
            routes.MapRoute(

                name: "Culture",

                url: "{culture}/{controller}/{action}",

                defaults: new
                {

                    culture = "zh-CN",

                    controller = "Culture",

                    action = "Index"
                }

            );

            routes.MapRoute(
                       "Default", // 路由名称
                       "{controller}/{action}/{id}", // 带有参数的 URL
                       new { controller = "Template", action = "List", id = UrlParameter.Optional } // 参数默认值
                   );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            //注册全局过滤器
            RegisterGlobalFilters(GlobalFilters.Filters);
            //注册路由规则
            RegisterRoutes(RouteTable.Routes);
            //注册定制的视图位置
            RegisterCustomViewLocations();
            //注册定制的客户端设置
            RegisterCustomAppSetting();

             //其他操作
   
           NinjectDependencyResolver dependencyResovler = new NinjectDependencyResolver();
   
           dependencyResovler.Register<ResourceReader, DefaultResourceReader>();
   
           DependencyResolver.SetResolver(dependencyResovler);
   
        }

        private static void RegisterCustomViewLocations()
        {
            // We can optionally enable the standard Razor view CS (or VB) locations 
            // so we don't always search our extra / custom areas first when 
            // searching for a view.
            EnhancedViewLocator.EnableStandardRazorCSLocations();


            // Next, add two extra locations to store / arrange our views.


            EnhancedViewLocator.AddFolder("views/shared/files");



            // Finally, have the locator install a custom view engine to manage the lookups.
            EnhancedViewLocator.Install(ControllerBuilder.Current);
        }

        private static void RegisterCustomAppSetting()
        {


        }

        protected void Application_BeginRequest()

     { 
 
         HttpContextBase contextWrapper = new HttpContextWrapper(HttpContext.Current);
 
         string culture = RouteTable.Routes.GetRouteData(contextWrapper).Values["culture"] as string;
 
         if (!string.IsNullOrEmpty(culture))
 
         {
 
             try
 
             {
 
                 CultureInfo cultureInfo                 = new CultureInfo(culture);
 
                 Thread.CurrentThread.CurrentCulture     = cultureInfo;
 
                 Thread.CurrentThread.CurrentUICulture   = cultureInfo;
 
             }
 
             catch {}
 
         }
 
     }
    }
}